import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:package_info_plus/package_info_plus.dart';

class DevicePackageInfo {
  static DevicePackageInfo get instance => _getInstance();

  // 静态私有成员，没有初始化
  static DevicePackageInfo? _instance;

  // 工厂模式 : 单例公开访问点
  factory DevicePackageInfo() => _getInstance();

  // 静态、同步、私有访问点
  static DevicePackageInfo _getInstance() {
    return _instance ??= DevicePackageInfo._init();
  }

  DevicePackageInfo._init();

  ///App名称
  Future<String> get getAppName async =>
      (await _packageInfoFromPlatform()).appName;

  ///App包名
  Future<String> getPackageName() async =>
      (await _packageInfoFromPlatform()).packageName;

  ///App版本号
  Future<String> getAppVersion() async =>
      (await _packageInfoFromPlatform()).version;

  ///App内部升级版本号
  Future<String> getBuildNumber() async =>
      (await _packageInfoFromPlatform()).buildNumber;

  ///手机品牌
  Future<String?> getBrand() async => Platform.isIOS
      ? (await iosDeviceInfo()).model
      : (await androidDeviceInfo()).brand;

  ///id
  Future<String?> getId() async => (await androidDeviceInfo()).id;

  ///手机型号
  Future<String?> getModel() async => Platform.isIOS
      ? (await iosDeviceInfo()).model
      : (await androidDeviceInfo()).model;

  ///当前手机系统版本
  Future<String?> getSystemVersion() async => Platform.isIOS
      ? (await iosDeviceInfo()).systemVersion
      : (await androidDeviceInfo()).version.release;

  ///分辨率
  Future<double?> widthPx() async =>
      (await androidDeviceInfo()).displayMetrics.widthPx;

  Future<double?> heightPx() async =>
      (await androidDeviceInfo()).displayMetrics.heightPx;

  //
  // ///系统名称
  // Future<String?> getPlatform() async =>
  //     Platform.isIOS ? (await iosDeviceInfo()).systemName : "android";

  // ///是不是物理设备
  // Future<bool> getIsPhysicalDevice() async => Platform.isIOS
  //     ? (await iosDeviceInfo()).isPhysicalDevice
  //     : (await androidDeviceInfo()).isPhysicalDevice;

  // ///用户唯一识别码
  // Future<String?> getUuid({Function? callback}) async => Platform.isIOS
  //     ? (await iosDeviceInfo()).identifierForVendor
  //     : (await androidDeviceInfo()).id;

  // ///手机具体的固件型号/Ui版本
  // Future<String?> getIncremental({Function? callback}) async => Platform.isIOS
  //     ? (await iosDeviceInfo()).systemVersion
  //     : (await androidDeviceInfo()).version.incremental;

  ///是否是华为或者荣耀手机
  Future<bool> isHuaweiOrHonorDevice() async {
    if (Platform.isAndroid) {
      var brand = (await androidDeviceInfo()).brand;
      return "HUAWEI" == brand.toUpperCase() || "HONOR" == brand.toUpperCase();
    }
    return false;
  }

  Future<PackageInfo> _packageInfoFromPlatform() {
    return PackageInfo.fromPlatform();
  }

  IosDeviceInfo? _iosDeviceInfo;

  Future<IosDeviceInfo> iosDeviceInfo() async {
    return _iosDeviceInfo ??= await DeviceInfoPlugin().iosInfo;
  }

  AndroidDeviceInfo? _androidInfo;

  Future<AndroidDeviceInfo> androidDeviceInfo() async {
    return _androidInfo ??= await DeviceInfoPlugin().androidInfo;
  }

// //获取运营商名称
// void getAndroidDeviceInfo() async {
//   final build = await androidDeviceInfo();
//   var map = <String, dynamic>{
//     'version.securityPatch': build.version.securityPatch,
//     'version.sdkInt': build.version.sdkInt,
//     'version.release': build.version.release,
//     'version.previewSdkInt': build.version.previewSdkInt,
//     'version.incremental': build.version.incremental,
//     'version.codename': build.version.codename,
//     'version.baseOS': build.version.baseOS,
//     'board': build.board,
//     'bootloader': build.bootloader,
//     'brand': build.brand,
//     'device': build.device,
//     'display': build.display,
//     'fingerprint': build.fingerprint,
//     'hardware': build.hardware,
//     'host': build.host,
//     'id': build.id,
//     'manufacturer': build.manufacturer,
//     'model': build.model,
//     'product': build.product,
//     //'name': build.name,
//     'supported32BitAbis': build.supported32BitAbis,
//     'supported64BitAbis': build.supported64BitAbis,
//     'supportedAbis': build.supportedAbis,
//     'tags': build.tags,
//     'type': build.type,
//     'isPhysicalDevice': build.isPhysicalDevice,
//     //'freeDiskSize': build.freeDiskSize,
//     //'totalDiskSize': build.totalDiskSize,
//     //'systemFeatures': build.systemFeatures,
//     'serialNumber': build.serialNumber,
//     //'isLowRamDevice': build.isLowRamDevice,
//     //'physicalRamSize': build.physicalRamSize,
//     //'availableRamSize': build.availableRamSize,
//   };
// }
}
